<?php
require('../sgfoot/start.php');
initAction('Index');

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/28
 * Time: 16:47
 */
class Index
{
    public function test()
    {
        echo phpinfo();
    }

    public function reg()
    {
        $check = $this->_check();
        if ($check['status'] != 0) {
            return $check;
        }
        $name   = input('post.name');
        $mobile = input('post.mobile');
        $data   = [
            'name'   => $name,
            'mobile' => $mobile,
        ];
        $bool   = db('record')->set($data)->insert();
        if ($bool) {
            return setJson(0, 'ok');
        }
        return setJson(100, '失败');
    }

    public function like()
    {
        $info = $this->_info();
        if (!$info) {
            return setJson(10, '');
        }
        $name   = input('post.name');
        $mobile = input('post.mobile');
        $data   = [
            'name'   => $name,
            'mobile' => $mobile,
        ];
        $bool   = db('record')->set($data)->insert();
        if ($bool) {
            return setJson(0, 'ok');
        }
        return setJson(100, '失败');
    }

    public function say()
    {

    }

    private function _check()
    {
        $openid = $_SESSION['openid'];
        $check  = db('record')->where('openid', $openid)->count();
        if ($check > 0) {
            return setResult(10, '已经录入过信息了');
        }
        return setResult(0, 'ok');
    }

    private function _info()
    {
        $openid = $_SESSION['openid'];
        $info   = db('record')->where('openid', $openid)->get();
        if (!$info) {
            return false;
        }
        return true;
    }

}